#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    int calculate(string s)
    {
        auto A = [](int &x, int &y) -> void
        {
            x = 2 * x + y;
        };
        auto B = [](int &x, int &y) -> void
        {
            y = 2 * y + x;
        };
        int x = 1, y = 0;

        for (char c : s)
        {
            if (c == 'A')
                A(x, y);
            else
                B(x, y);
        }
        return x + y;
    }

    int calculate_2(string s)
    {
        int x = 1, y = 0;
        for (auto &c : s)
        {
            if (c == 'A')
            {
                x = x + x + y;
            }
            else
            {
                y = y + y + x;
            }
        }
        return x + y;
    }
};
int main()
{
    Solution s;
    cout << s.calculate("AB") << endl;
    system("pause");
    return 0;
}
